<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.MatchString"
              clearable
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableSortRef"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        width="auto"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <template v-if="item.label == '设备列表'">
            <span v-for="item in scope.row.DeviceList" :key="item">
              {{ item }}
            </span>
          </template>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="200"
      >
        <template slot-scope="scope">
          <el-button text @click="getDetail(scope.row)">详情</el-button>
          <el-button text type="danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-dialog :close-on-click-modal="false"  :visible="dialogVisible" title="添加" width="30%">
      <el-form :model="form">
        <el-form-item label="保养方案名称" :label-width="formLabelWidth">
          <el-input v-model="form.Name" :rows="2" type="text" />
        </el-form-item>
        <el-form-item label="设备列表" :label-width="formLabelWidth">
          <el-select
            v-model="form.DeviceList"
            multiple
            collapse-tags
            placeholder="设备"
          >
            <el-option
              :label="item"
              :value="item"
              v-for="item in DeviceTypes"
              :key="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="保养类型" :label-width="formLabelWidth">
          <el-radio-group v-model="form.MainrenanceType">
            <el-radio label="1">次数</el-radio>
            <el-radio label="2">周期</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="form.MainrenanceType == 2"
          label="保养频次安排"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.Cycle" placeholder="保养频次安排">
            <el-option label="每日" value="每日" />
            <el-option label="每周" value="每周" />
            <el-option label="每月" value="每月" />
            <el-option label="每季度" value="每季度" />
            <el-option label="每年" value="每年" />
          </el-select>
        </el-form-item>
        <el-form-item label="保养内容" :label-width="formLabelWidth">
          <el-input v-model="form.Content" :rows="5" type="textarea" />
        </el-form-item>
        <el-form-item label="开始时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="form.StartTime"
            type="date"
            placeholder="请选择开始时间"
          />
        </el-form-item>
        <!-- <el-form-item label="保养周期" v-if="form.MainrenanceType != 2" :label-width="formLabelWidth">
          <el-input v-model="form.Cycle" :rows="2" type="text" />
        </el-form-item> -->
        <el-form-item label="负责人" :label-width="formLabelWidth">
          <el-input v-model="form.Director" :rows="2" type="text" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submit">保存</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" 
      :visible="dialogVisible1"
      title="保养方案内容"
      width="50%"
      :before-close="handleClose1"
    >
      <div class="home">
        <el-descriptions direction="vertical" :column="2" border>
          <el-descriptions-item label="">
            <template slot="label">
              <i class="el-icon-user"></i>
              保养方案
            </template>
            {{ detail.MainrenancePlanId }}
          </el-descriptions-item>
          <el-descriptions-item label="负责人">
            <template slot="label">
              <i class="el-icon-user"></i>
              负责人
            </template>
            {{ detail.Director }}
          </el-descriptions-item>
        </el-descriptions>
        <el-row :gutter="20" style="margin-top: 30px">
          <el-col :span="24">
            <el-descriptions direction="vertical" :column="3" border>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"></i>
                  设备类型
                </template>
                <el-tag
                  size="small"
                  v-for="(item, index) in detail.DeviceList"
                  :key="index"
                >
                  {{ item }}
                </el-tag>
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"></i>
                  保养类型
                </template>
                {{ detail.MainrenanceType }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"></i>
                  保养频次安排
                </template>
                {{ detail.Cycle }}
              </el-descriptions-item>
            </el-descriptions>
          </el-col>
          <el-col :span="24">
            <el-row :gutter="20">
              <el-col :span="24">
                <el-descriptions direction="vertical" :column="1" border>
                  <el-descriptions-item label="">
                    <template slot="label">
                      <i class="el-icon-user"></i>
                      保养内容以及要求
                    </template>
                    <div>
                      <p>{{ detail.Content }}</p>
                    </div>
                  </el-descriptions-item>
                  <el-descriptions-item label="">
                    <template slot="label">
                      <i class="el-icon-user"></i>
                      备注
                    </template>
                    <div>暂无内容</div>
                  </el-descriptions-item>
                </el-descriptions>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </div>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="dialogVisible1 = false">取消</el-button>
          <!-- <el-button type="primary" @click="submit">保存</el-button> -->
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {
    PlanManagerNewly,
    MainrenancePlanList,
    AllDeviceld,
    MainrenancePlanIdDelete,
  } from '@/api/material'

  export default {
    data() {
      return {
        TrayInfoData: [],
        form: {
          Id: '',
          MaterialType: '',
          Content: '',
          ApplyProduct: '',
          Creator: '',
          CreateTime: new Date(),
          MaterialNum: '',
        },
        radio: 3,
        formLabelWidth: '100px',
        dialogVisible: false,
        dialogVisible1: false,
        tableSortRef: null,
        editRef: null,
        border: true,
        height: this.$baseTableHeight(1),
        stripe: false,
        lineHeight: 'small',
        isFullscreen: false,
        checkList: [
          '保养编号',
          '保养名称',
          '设备列表',
          '保养内容',
          '保养类型',
          '保养频次',
          '开始时间',
          '负责人',
        ],
        columns: [
          {
            label: '保养编号',
            prop: 'MainrenancePlanId',
            disableCheck: true,
          },
          {
            label: '保养名称',
            prop: 'Name',
            disableCheck: true,
          },
          {
            label: '设备列表',
            prop: 'DeviceList',
            disableCheck: true,
          },
          {
            label: '保养内容',
            prop: 'Content',
          },
          {
            label: '保养类型',
            prop: 'MainrenanceType',
          },
          {
            label: '保养频次',
            prop: 'Cycle',
          },
          {
            label: '开始时间',
            prop: 'StartTime',
          },
          {
            label: '负责人',
            prop: 'Director',
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          MatchString: '',
        },
        title: '添加',
        productLineData: [],
        ProductInfoData: [],

        DeviceTypes: [],
        detail: {},
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
      AllDeviceld().then((res) => {
        console.log('所有设备', res)
        this.DeviceTypes = res.DeviceIds
      })
    },
    methods: {
      getDetail(row) {
        this.detail = row
        this.dialogVisible1 = true
      },
      handleLine(row) {
        this.dialogVisible = true
        this.productLineData = row.ProductLine
      },
      handleSelectionChange(e) {
        this.form.ApplyProduct = e.map((item) => {
          return item.ProducationName
        })
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      submit() {
        let param = { ...this.form }
        this.dialogVisible = false
        param.StartTime = this.days(param.StartTime)
        param.MainrenancePlanId = 'MP' + new Date().getTime()
        param.MainrenanceType = param.MainrenanceType == 1 ? '单次' : '周期'
        param.Cycle = param.MainrenanceType == '单次' ? '单次' : param.Cycle
        console.log('新增提交', param)

        PlanManagerNewly(param).then((res) => {
          console.log('PlanManagerNewly', res)
          this.$baseMessage('提交成功', 'success', 'vab-hey-message-success')
          this.fetchData()
        })
      },
      handleEdit() {
        this.dialogVisible = true
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await MainrenancePlanIdDelete({
            MainrenancePlanId: row.MainrenancePlanId,
          })
          // $baseMessage(msg, 'success', 'vab-hey-message-success')
          await this.fetchData()
        })
        return false
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleClose1() {
        this.dialogVisible1 = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const params = {
          Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
          Count: this.queryForm.pageSize,
          MatchString: this.queryForm.MatchString,
        }
        MainrenancePlanList(params).then((res) => {
          this.list = res.MainrenancePlanList
          this.total = res.TotalCount
          this.listLoading = false
        })
      },
    },
  }
</script>
